/*    file: CRtpFileWriter.cpp
 *    desc:
 * 
 * created: 2017-04-15
 *  author: chuanjiang.zh@qq.com
 * company: 
 */

#include "CRtpFileWriter.h"

CRtpFileWriter::CRtpFileWriter():
	m_file()
{
}

CRtpFileWriter::~CRtpFileWriter()
{
	close();
}

bool CRtpFileWriter::open(const char* filename)
{
	if (!filename || (strlen(filename) == 0))
	{
		return false;
	}

	m_file = fopen(filename, "ab");

	return (m_file != NULL);
}

void CRtpFileWriter::close()
{
	if (m_file)
	{
		fclose(m_file);
		m_file = NULL;
	}
}

bool CRtpFileWriter::isOpen()
{
	return (m_file != NULL);
}

bool CRtpFileWriter::write(unsigned char* buffer, int length, uint32_t timestamp, bool marker)
{
	if (!buffer || (length <= 0))
	{
		return false;
	}

	bool done = false;
	if (m_file != NULL)
	{
		size_t written = fwrite(buffer, 1, length, m_file);
		done = (written == length);

		fflush(m_file);
	}

	return done;
}

bool CRtpFileWriter::setFile(const char* filepath)
{
	if (m_file)
	{
		fclose(m_file);
		m_file = NULL;
	}

	m_file = fopen(filepath, "wb");

	return (m_file != NULL);
}
